<?php

namespace App\Listener;

use App\Event\RegDoneEvent;
use App\Model\Ocpc360Search;
use App\Model\OcpcKshou;
use App\Model\OcpcKshouUser;
use App\Model\Spread\SpreadLog;
use App\Model\User;
use App\Model\UserProfile;
use App\Service\ReqService;
use GuzzleHttp\Client;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Event\Annotation\Listener;

/**
 * cpa激活
 * @Listener
 */
class CpaActivateListener implements ListenerInterface
{
    const key_360 = 'wo14uwa7t3sgby6rqhxe';

    const SECRET_360 = 'uo2hdi8hx6qw4keqhdy5csktm5fx1fsv';

    public function listen(): array
    {
        // 返回一个该监听器要监听的事件数组，可以同时监听多个事件
        return [
            //注册事件
            RegDoneEvent::class,
        ];
    }

    public function process(object $event)
    {
        if (!$event->userId && $event->userId == 0) {
            return;
        }

        $data = SpreadLog::where('user_id',$event->userId)->first();

        $userInfo = User::where('id', $event->userId)->first();
        log_info(__CLASS__.json_encode($userInfo->toArray()));

        if ($userInfo->channel == 1013) {
            $info = $userInfo->toArray();
            $log = OcpcKshou::handleSearch($info);
            if (!empty($log) && isset($log['channel']) && $log['channel'] == 1013) {
                log_info(__CLASS__ . "==relationKshoulog==" . json_encode($log) . "===false");
                return;
            }
        }

        if (empty($data)) {
            return;
        }

        if ($data->is_activate == 1)
        {
            return;
        }

        if ($data->s_id ==59) {
            $this->ocpc360Search();
        }

//        $data = $data->toArray();

        $data->is_activate = 1;

        $data->save();
    }

    public function ocpc360Search()
    {
        $ocpcData = Ocpc360Search::getData();

        if (empty($ocpcData)) {
            return;
        }

        $param = [
            'data' => [
                'request_time' => time(),
                'data_industry' => 'ocpc_ms_convert',
                'data_detail' => [
                    'qhclickid' => $ocpcData->qhclickid,
                    'event' => 'REGISTERED'
                ]
            ]
        ];

        $postData = json_encode($param);

        $sign = md5(self::SECRET_360.$postData);

        $url = 'https://convert.dop.360.cn/uploadWebConvert';

        $UrlInfo = parse_url($url);
        $domain = $UrlInfo['scheme'] . '://'.$UrlInfo['host'];
        $options = ['base_uri' => $domain];

        // $client 为协程化的 GuzzleHttp\Client 对象
        $client = (new Client($options));

//        $urlPath= ;

        $urlPath = isset($UrlInfo['query']) ? $UrlInfo['path']. '?' . $UrlInfo['query'] : $UrlInfo['path'];

        log_info(__CLASS__.__FUNCTION__.':==:'.$domain.$urlPath);

        $response = $client->request('POST', $urlPath, [
            'headers' => [
                'App-Key' => self::key_360,
                'App-Sign' => $sign,
                'Content-Type' => 'application/json; charset=utf-8',
                'Content-Length' => strlen($postData)
            ],
            'body' => $postData
        ]);

        $result = $response->getBody()->getContents();

        $result = json_decode($result);
        log_info(__CLASS__.__FUNCTION__.'360回调:'.json_encode($result));
        if ($result->error != 'Success') {
            return;
        }

        $ocpcData->flag = 2;
        $ocpcData->save();
    }
}